package com.thread.thread.java8;

import javax.sound.midi.Soundbank;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/*
* 方法引用
* */


class Car {
	//Supplier是jdk1.8的接口，这里和lamda一起使用了
	public static Integer create(final Supplier<Integer> supplier) {
		return supplier.get();
	}

	public static void collide(final Integer num) {
		System.out.println("Collided " + num);
	}

	public  Integer follow(final Function<Integer,Integer> another,Integer number) {
		System.out.println("Following the " + another.toString());
		return another.apply(number);
	}


	public Integer repair(Predicate<Integer> repair,Integer a) {
		System.out.println("Repaired " + this.toString());
		if(repair.test(a)){
			return 1;
		}else{
			return 0;
		}
	}
}
public class Test1 {
	public static void main(String[] args) {

		Supplier<Integer> supplier=()->{return 1+1;};
		System.out.println(supplier.get());

		//Supplier 无参数，返回一个结果
		Integer collide = Car.create(supplier);
		System.out.println(collide);


		//Consumer 接受一个输入参数并且无返回的操作
		Consumer<Integer> collide1 = Car::collide;
		collide1.accept(1);

		//Function 接受一个输入参数，返回一个结果
		Function<Integer,Integer> function=(x)->{
			int a=x+1;
			return a;
		};
		Car car=new Car();
		Integer follow=car.follow(function,2);
		System.out.println(follow);

		//Predicate 接收一个参数，返回一个boolean的结果
		Predicate<Integer> predicate=(x)->{
			if(x==0){
				return false;
			}else{
				return true;
			}
		};


		Integer repair = car.repair(predicate, 0);
		System.out.println(repair);

	}

}
